package com.capgemini.fs.presentation.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;

public class CommonMethods {
	// Read JSON By Request
	public static String readJSONRequest(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		String line;
		try {
			BufferedReader reader = request.getReader();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static Date formateStringToDate(String source) {
		DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
		Date date = null;
		try {
			date = df.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date formateStringToDateAddHMS(String source) {
		DateFormat df = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
		Date date = null;
		try {
			date = df.parse(source + " 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String formateDateToString(Date olddate) {
		java.text.DateFormat format = new java.text.SimpleDateFormat(
				"MM-dd-yyyy");
		String newDate = format.format(olddate);
		return newDate;
	}

	public static String getExamStatus(int id) {
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("0", "Open");
		hm.put("1", "Taken");
		hm.put("2", "Expired");
		hm.put("3", "In Progress");
		hm.put("4", "Exception");
		String statusId = String.valueOf(id);
		return hm.get(statusId);
	}

	public static long fromDateStringToLong(String inVal) {
		Date date = null;
		SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
		try {
			date = inputFormat.parse(inVal);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

	public static long calculateDifferential(Date start, Date end) {
		long startT = start.getTime();
		long endT = end.getTime();
		long durationS = (endT - startT) / (1000);
		return durationS;
	}

	public static String formatDate(java.util.Date date) {
		String result = "";
		java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		// "yyyy-MM-dd HH:MM:ss");
		result = dateFormat.format(date);
		return result;
	}
}
